let URL = 'https://sales.yunpintech.com';
// //#ifndef MP-WEIXIN

// if (process.env.NODE_ENV === 'development') {
// 	console.log('开发环境');
// 	URL = 'https://sales.cqmomo.top/';
// } else {
// 	console.log('生产环境');
// 	URL = 'https://sales.cqmomo.top/';
	
// }
// // #endif
// // #ifdef MP-WEIXIN
// URL = 'http://api.leanplants.com/';
// // #endif
switch (uni.getSystemInfoSync().platform) {
	case 'android':
		console.log('运行Android上')
		break;
	case 'ios':
		console.log('运行iOS上')
		break;
	default:
		console.log('运行在开发者工具上')
		break;
};

const http = (url, data, method) => {
	
	let token = uni.getStorageSync('token');
	// let token = '6ca251509672286b262e7209b708bef92d5f0d39';
	// if (token && token != '') {
	// 	data = {
	// 		...data,
	// 		token: token
	// 	}
	// };
	uni.showLoading({
		title:'加载中'
	});
	console.log(data);
	return new Promise(function(resolve, reject) {
		uni.request({
			url: URL + url || '',
			method: method || 'POST',
			data: data || null,
			header: {
				"Content-Type": "application/x-www-form-urlencoded", // application/json
				// "Access-Token":uni.getStorageSync('jwt') || '',
				"Authorization":token||''
			},
			success: function(res) {
				console.log(res)
				uni.hideLoading();
				if(res.data.code==401){
					uni.showToast({
						title:'登录失效',
						icon:'none'
					})
					setTimeout(()=>{
						uni.setStorageSync('token', '');
						uni.reLaunch({
							url: '/pages/logon/logon'
						})
					},1500)
				}else{
					resolve(res.data);
				}
			},
			fail: function(err) {
				uni.hideLoading();
				console.log(data);
				console.log(JSON.stringify(err));
				reject({
					error: '网络错误'
				});
			},
		})
	})
}

export default http;
